<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <img src="@/assets/logo.png" alt="" />
        <span>电商管理系统</span>
      </div>
      <div class="btn">
        <el-button type="info" @click="goOut">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse?'64px':'200px'"
        >
        <div class="toller-btn" @click="tollerAction">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="menuDefaultActive"
        >
          <el-submenu :index="item.id+''" v-for="item in meunList" :key="item.id">
            <template slot="title">
              <i :class="icon[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu></el-aside
      >
      <!-- 内容 -->
      <el-main >
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import axios from '@/axios/axios'
export default {
  name:'Home',
  data(){
    return {
      meunList:[],   //菜单栏数据
      iscollapse:false,
      icon:{
        '125':'iconfont icon-yonghu',
        '103':'iconfont icon-quanxian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-dingdan',
        '145':'iconfont icon-shuju'
      },
      menuDefaultActive:'/welcome'    //默认选中
    }
  },
  created(){
    this.getMenu()
  },
  methods: {
    goOut() {
      sessionStorage.removeItem('TOKEN')
      this.$router.replace('/login')
    },
    //左边菜单栏的数据请求
   async getMenu(){
    const {data:{data}} = await axios.get('/menus')
    //console.log(data);
      if(data){
        this.meunList=data
      }
    },
    //菜单折叠和展开
    tollerAction(){
      this.iscollapse=!this.iscollapse
    }
  },
  watch:{
    $route:{
      handler(newVal){
        this.menuDefaultActive=newVal.path
      },
      immediate:true
    }
  }
}
</script>

<style lang="scss" scoped>
.home-container {
  height: 100%;
  > .el-header {
    background-color: #363d40;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-aside {
    background-color: #313743;
    .el-menu{
      border-right:none
    }
  }
}
.header-left {
  display: flex;
  align-items: center;
  color:#fff;
  > img {
    width: 50px;
  }
}
.toller-btn{
  color:#fff;
  text-align: center;
  letter-spacing: 2px;
  background-color: #475163;
  cursor: pointer;
}
.iconfont{
  margin-right:5px;
}
</style>